package com.leyou.common.advice;

import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.vo.ExceptionResult;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

//通用的异常通知-- 返回自己定义的返回的异常
//默认通知所有带Controller注解
@ControllerAdvice
public class CommonExceptionHandler {

    //@ExceptionHandler(RuntimeException.class)  这里RuntimeException 是spring自带的异常
    @ExceptionHandler(LyException.class)//这里的LyException是自定义的异常
    public ResponseEntity<ExceptionResult> handleExeption(LyException e){
        ExceptionEnum em = e.getExceptionEnum();
        return ResponseEntity.status(e.getExceptionEnum().getCode()).body(new ExceptionResult(e.getExceptionEnum()));
    }
}
